import React, { Suspense } from "react";
import { Spin } from "antd";
import { Navigate, useLocation } from "react-router-dom";

const AuthRouter = ({ children }: any) => {
  const location = useLocation();
  const token = localStorage.token;
  if (!token && location.pathname !== "/login") {
    return <Navigate to="/login" />;
  } else {
    return <>{children}</>;
  }
};

export const wrapSuspense = (Component: any) => (props: any) => (
  <AuthRouter>
    <Suspense fallback={<Spin size="large" />}>
      <Component {...props} />
    </Suspense>
  </AuthRouter>
);